using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel.Design;

namespace LeisureCenter.Controls
{
    public class ToolButton : LeisureCenterControl
    {
        public ToolButton()
        {
            Paint += new PaintEventHandler(ToolButton_Paint);
        }

        private bool pressed;

        private bool down;

        public bool Down
        {
            get { return down; }
            set
            {
                down = value;
                Invalidate();
            }
        }

        //[EditorBrowsable(EditorBrowsableState.Advanced)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                Invalidate();
            }
        }

        private void ToolButton_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;

            Rectangle rect = ClientRectangle;
            rect.X += Padding.Left;
            rect.Width -= 1 + Padding.Left + Padding.Right;
            rect.Y += Padding.Top;
            rect.Height -= 1 + Padding.Top + Padding.Bottom;

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            Brush brush;
            Pen pen;
            if (Focused)
            {
                brush = new SolidBrush(Color.Black);
                pen = new Pen(Color.Black);
            }
            else
            {
                brush = SystemBrushes.ButtonShadow;
                pen = SystemPens.ButtonShadow;
            }

            if (down || pressed)
            {
                Panel.FillRoundRect(e.Graphics, rect, brush);
                rect.Y += 2;
                e.Graphics.DrawString(Text, Font, new SolidBrush(BackColor), rect, format);
            }
            else
            {
                Panel.DrawRoundRect(e.Graphics, rect, pen);
                rect.Y += 2;
                e.Graphics.DrawString(Text, Font, brush, rect, format);
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ' || e.KeyChar == 13)
            {
                OnClick(null);
                return;
            }
            base.OnKeyPress(e);
        }
    }
}
